﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Security;
using TripLOA.GUI.internationalization;
using TripLOA.GUI.FullTextSearch;

namespace TripLOA.GUI
{
    public partial class GUIMaster1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnLangIt_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton senderLink = sender as ImageButton;

            //store requested language as new culture in the session
            Session[LOAGlobal.Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;
            
            //reload last requested page with new culture
            Server.Transfer(Request.Path);
        }

        protected void btnLangIt_Init(object sender, EventArgs e)
        {
            //ImageButton senderLink = sender as ImageButton;
            //senderLink.PostBackUrl = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;


            ImageButton senderLink = sender as ImageButton;
            senderLink.PostBackUrl = HttpContext.Current.Request.Url.PathAndQuery;
        }

        protected void btnLangEn_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton senderLink = sender as ImageButton;

            //store requested language as new culture in the session
            Session[LOAGlobal.Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;
          
            //reload last requested page with new culture
            Server.Transfer(Request.Path);
        }

        protected void btnLangEn_Init(object sender, EventArgs e)
        {
            //ImageButton senderLink = sender as ImageButton;
            //senderLink.PostBackUrl = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;

            ImageButton senderLink = sender as ImageButton;
            senderLink.PostBackUrl = HttpContext.Current.Request.Url.PathAndQuery;
        }

        protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            TripLoaUserManager.Instance.getUser(Membership.GetUser().UserName).signOut();
            
            FormsAuthentication.SignOut();

            Response.Redirect("/Default.aspx");
            //Request.UrlReferrer.ToString()
        }


        protected void btnFulltext_Click(object sender, EventArgs e)
        {
            string query = txtFulltext.Text;
            query = query.Trim();

            if (!query.Equals(""))
            {
                // Stores the query string in the FullTextSearchProperties object
                ((FullTextSearchProperties)Session[LOAGlobal.Global.SESSION_KEY_FTSEARCH_PROPERTIES]).Query = query;

                // Redirection to the search page
                Response.Redirect("/FullTextSearch.aspx");
            }
        }


/*        protected void Login_OnAutenticate(Object sender, EventArgs e)
        {
            try
            {
            }
            catch (Storage.Exceptions.UserNotFoundException exceptionLoginError)
            {
                //TODO: Segnare l'evento nel logger
            }
        }*/

        protected void LoginError(object sender, EventArgs e)
        {
            
                Login log = (Login)this.LoginView1.FindControl("Login1");

                ClientScriptManager managerScript = Page.ClientScript;
                managerScript.RegisterStartupScript(this.GetType(), "prova", "viewError(\"" + log.FailureText + "\");", true);
        }
    }
}
